import XEUtils from 'xe-utils'

// 状态对应图标
export const todoStatusIcon = {
  '001': 'no-audit',
  '002': 'has-audit',
  '004': 'has-back',
  '008': 'has-used',
  '0': 'no-audit',
  '1': 'has-used'
}
export const expertStatusIcon = {
  '001': 'review',
  '002': 'has-check'
}
// 项目申报展示状态
export const applyAllowStatus = [ '001', '004' ]
// 项目上报展示状态
export const reportAllowStatus = [ '001', '004' ]

const HomeUtils = {
  tranfReserveData: (reserveData) => {
    let applyItems = []
    let reportItems = []
    reserveData.forEach(resItem => {
      resItem.list.map(item => item['icon'] = todoStatusIcon[item.status])
      if(resItem.businessType === '003') {
        let newItem = {
          name: resItem.title,
          infoList: resItem.list.filter(item => reportAllowStatus.includes(item.status))
        }
        reportItems.push(newItem)
      } else if (resItem.businessType === '001') {
        let newItem = {
          name: resItem.title,
          infoList: resItem.list.filter(item => applyAllowStatus.includes(item.status))
        }
        applyItems.push(newItem)
      }
    })
    return {
      applyItems, reportItems
    }
  },
  tranfStandbyData: (standbyData) => {
    let applyItems = []
    let reportItems = []
    standbyData.forEach(resItem => {
      resItem.list.map(item => item['icon'] = todoStatusIcon[item.status])
      if(resItem.businessType === '003') {
        let newItem = {
          name: resItem.title,
          infoList: resItem.list.filter(item => reportAllowStatus.includes(item.status))
        }
        reportItems.push(newItem)
      } else if (resItem.businessType === '001') {
        let newItem = {
          name: resItem.title,
          infoList: resItem.list.filter(item => applyAllowStatus.includes(item.status))
        }
        applyItems.push(newItem)
      }
    })
    return {
      applyItems, reportItems
    }
  },
  tranfSortProject: (sortProjData) => {
    let noSortItem = {
      name: '未排序',
      infoList: []
    }
    sortProjData.forEach(sortItem => {
      // 处理未排序
      let filterItems = sortItem.list.filter(item => item.status === '0')
      let newItem = filterItems[0]
      newItem.label = sortItem.title
      noSortItem.infoList.push(newItem)
    })
    return {
      sortItems: [ noSortItem ]
    }
  },
  /**
   * 处理审核数据
   * @param {Object} uTodoData 待办数据
   * @returns Object
   */
  tranfAuditData: (uTodoData) => {
    // 待审核项目数据
    let resultAuditItems = []
    // 待审核下级项目数据
    let resultBelowAuditItems = []
    // 项目入库数据
    let resultWarehouseItems = []
    // 储备项目
    const reserveData = uTodoData.resProjectAudit
    if (reserveData) {
      reserveData.forEach(item => {
        // 项目审核
        if (item.businessType === '002') {
          // 未审核
          let filterItems = item.list.filter(item => item.status === '001')
          let newItem = filterItems[0]
          newItem.label = item.title.replace('审核', '')
          newItem['icon'] = 'reserve'
          resultAuditItems.push(newItem)
        }
        // 下级项目审核
        if (item.businessType === '005') {
          // 未审核
          let filterItems = item.list.filter(item => item.status === '001')
          let newItem = filterItems[0]
          newItem.label = '储备项目'
          newItem['icon'] = 'reserve'
          resultBelowAuditItems.push(newItem)
        }
        // 项目入库审核
        if (item.businessType === '006') {
          // 未审核
          let filterItems = item.list.filter(item => item.status === '001')
          let newItem = filterItems[0]
          newItem.label = '储备项目'
          newItem['icon'] = 'reserve'
          resultWarehouseItems.push(newItem)
        }
      })
    }
    // 备选项目
    const standbyData = uTodoData.altProjectAudit
    if (standbyData) {
      standbyData.forEach(item => {
        // 项目审核
        if (item.businessType === '002') {
          // 未审核
          let filterItems = item.list.filter(item => item.status === '001')
          let newItem = filterItems[0]
          newItem.label = item.title.replace('审核', '')
          newItem['icon'] = 'standby'
          resultAuditItems.push(newItem)
        }
        // 下级项目审核
        if (item.businessType === '005') {
          // 未审核
          let filterItems = item.list.filter(item => item.status === '001')
          let newItem = filterItems[0]
          newItem.label = '备选项目'
          newItem['icon'] = 'standby'
          resultBelowAuditItems.push(newItem)
        }
        // 项目入库审核
        if (item.businessType === '006') {
          // 未审核
          let filterItems = item.list.filter(item => item.status === '001')
          let newItem = filterItems[0]
          newItem.label = '备选项目'
          newItem['icon'] = 'standby'
          resultWarehouseItems.push(newItem)
        }
      })
    }
    return {
      auditItems: resultAuditItems,
      warehouseItems: resultWarehouseItems,
      belowAuditItems: resultBelowAuditItems,
    }
  },
  /**
   * 处理经办数据
   * @param {Object} uTodoData 待办数据
   * @returns Object
   */
  tranfHandleData: (uTodoData) => {
    let resultSortItems = []
    let resultApplyItems = []
    let resultReportItems = []
    if (!XEUtils.isEmpty(uTodoData)) {
      // 储备项目
      const reserveData = uTodoData.resProjectApply
      if (reserveData) {
        const { applyItems, reportItems } = HomeUtils.tranfReserveData(reserveData)
        resultApplyItems = resultApplyItems.concat(applyItems)
        resultReportItems = resultReportItems.concat(reportItems)
      }

      // 备选项目
      const standbyData = uTodoData.altProjectApply
      if (standbyData) {
        const { applyItems, reportItems } = HomeUtils.tranfStandbyData(standbyData)
        resultApplyItems = resultApplyItems.concat(applyItems)
        resultReportItems = resultReportItems.concat(reportItems)
      }

      // 项目排序
      const sortProjectData = uTodoData.sortProject
      if (sortProjectData) {
        const { sortItems } = HomeUtils.tranfSortProject(sortProjectData)
        resultSortItems = sortItems
      }
    }
    return {
      sortItems: resultSortItems,
      applyItems: resultApplyItems,
      reportItems: resultReportItems
    }
  },
  /**
   * 处理单位数据
   * @param {Object} uTodoData 待办数据
   * @returns Object
   */
  tranfUnitData: (uTodoData) => {
    let resultSortItems = []
    let resultApplyItems = []
    let resultReportItems = []
    if (!XEUtils.isEmpty(uTodoData)) {
      // 储备项目
      const reserveData = uTodoData.resProjectUnit
      if (reserveData) {
        const { applyItems, reportItems } = HomeUtils.tranfReserveData(reserveData)
        resultApplyItems = resultApplyItems.concat(applyItems)
        resultReportItems = resultReportItems.concat(reportItems)
      }

      // 备选项目
      const standbyData = uTodoData.altProjectUnit
      if (standbyData) {
        const { applyItems, reportItems } = HomeUtils.tranfStandbyData(standbyData)
        resultApplyItems = resultApplyItems.concat(applyItems)
        resultReportItems = resultReportItems.concat(reportItems)
      }
    }
    return {
      sortItems: resultSortItems,
      applyItems: resultApplyItems,
      reportItems: resultReportItems
    }
  },
  /**
   * 处理机构数据
   * @param {Object} uTodoData 待办数据
   * @returns Object
   */
  tranfAgencyData: (uTodoData) => {
    let resultReviewProjItem = {}
    let resultReviewTaskItem = {}
    let resultReserveItem = {}
    let resultStandbyItem = {}
    if (!XEUtils.isEmpty(uTodoData)) {
      // 评审项目
      const reviewProjData = uTodoData.reviewProject
      if (reviewProjData) {
        reviewProjData.forEach(resItem => {
          resItem.list.map(item => item['icon'] = todoStatusIcon[item.status])
          resultReviewProjItem = {
            name: resItem.title,
            infoList: resItem.list.filter(item => ['0', '1'].includes(item.status))
          }
        })
      }
      // 评审任务
      const reviewTaskData = uTodoData.reviewTask
      if (reviewTaskData) {
        reviewTaskData.forEach(resItem => {
          resItem.list.map(item => item['icon'] = todoStatusIcon[item.status])
          resultReviewTaskItem = {
            name: resItem.title,
            infoList: resItem.list.filter(item => ['0', '1'].includes(item.status))
          }
        })
      }
      // 储备项目
      const reserveData = uTodoData.resProjectAgency
      if (reserveData) {
        reserveData.forEach(resItem => {
          resItem.list.map(item => item['icon'] = todoStatusIcon[item.status])
          resultReserveItem = {
            name: resItem.title,
            bgColor: ['#737bfc', '#3dadf6'],
            infoList: resItem.list.filter(item => applyAllowStatus.includes(item.status))
          }
        })
      }

      // 备选项目
      const standbyData = uTodoData.altProjectAgency
      if (standbyData) {
        standbyData.forEach(resItem => {
          resItem.list.map(item => item['icon'] = todoStatusIcon[item.status])
          resultStandbyItem = {
            name: resItem.title,
            bgColor: ['#ea677c', '#ef9b5f'],
            infoList: resItem.list.filter(item => applyAllowStatus.includes(item.status))
          }
        })
      }
    }
    return {
      resultReviewProjItem, resultReviewTaskItem,
      resultReserveItem, resultStandbyItem
    }
  },
  /**
   * 处理经办数据
   * @param {Object} uTodoData 待办数据
   * @returns Object
   */
  tranfExpertData: (uTodoData) => {
    let expertItems = []
    if (!XEUtils.isEmpty(uTodoData)) {
      // 专家评审
      const expertReviewData = uTodoData.expertReview
      expertReviewData.forEach(resItem => {
        resItem.list.map(item => item['icon'] = expertStatusIcon[item.status] || '')
        if (resItem.businessType === '012') {
          expertItems = expertItems.concat(resItem.list.filter(item => ['001', '002'].includes(item.status)))
        }
      })
      // 评审确认
      const expertSubmitData = uTodoData.expertSubmit
      expertSubmitData.forEach(resItem => {
        resItem.list.map(item => item['icon'] = expertStatusIcon[item.status] || '')
        if (resItem.businessType === '013') {
          expertItems = expertItems.concat(resItem.list.filter(item => ['001', '002'].includes(item.status)))
        }
      })
    }
    return {
      expertItems
    }
  },
  /**
   * 处理经办数据
   * @param {Object} uTodoData 待办数据
   * @returns Object
   */
  tranfLawAccData: (uTodoData) => {
    let lawItems = []
    let accItems = []
    if (!XEUtils.isEmpty(uTodoData)) {
      // 律所
      const lawProjectData = uTodoData.lawProject
      if (lawProjectData) {
        lawProjectData.forEach(resItem => {
          resItem.list.map(item => item['icon'] = 'report-upload')
          if (resItem.businessType === '011') {
            lawItems = resItem.list
          }
        })
      }
      // 会所
      const clubProjectData = uTodoData.clubProject
      if (clubProjectData) {
        clubProjectData.forEach(resItem => {
          resItem.list.map(item => item['icon'] = 'report-upload')
          if (resItem.businessType === '010') {
            accItems = resItem.list
          }
        })
      }
    }
    return {
      lawItems, accItems
    }
  }
}

export default HomeUtils
